﻿layui.use(['form', 'laydate', 'layer'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var $ = layui.$;
    laydate.render({
        elem: '#measureDate'
    });

    // 提交事件
    form.on('submit(demo1)', function () {
        var inputValues = {
            measureDate: $("#measureDate").val(),
            sheepGender: $("#sheepGender").val() == "true" ? true : false,
            sheepBreed: $("#sheepBreed").val(),
            cottageId: $("#cottageId").val(),
            field: $("#field").val(),
            earNumber: $("#earNumber").val(),
            height: $("#height").val(),
            bodyLength: $("#bodyLength").val(),
            chestWidth: $("#chestWidth").val(),
            cannonCircumference: $("#cannonCircumference").val(),
            scrotalCircumference: $("#scrotalCircumference").val(),
            backfatThickness: $("#backfatThickness").val(),
            eyeMuscleArea: $("#eyeMuscleArea").val(),
            eyeMuscleWidth: $("#eyeMuscleWidth").val(),
            CreateBy: "张三",
            CreateDate: new Date(),
        }

        // 发起Ajax请求新增数据
        $.ajax({
            url: 'http://10.31.60.18:8000//BreedingsSetRout/api/BodyMeasurement/CreateBodyMeasurement',
            type: 'post',
            data: JSON.stringify(inputValues), // 将数据转换为JSON格式
            contentType: 'application/json',
            success: function (response) {
                if (response.code == 200) {
                    layer.msg(response.msg, { icon: 1 });
                    setTimeout(function () {
                        location.href = '/Breedings/ShowBodyMeasurement';
                    }, 1000)
                } else {
                    layer.msg(response.msg, { icon: 2 });
                }
            },
            error: function () {
                // 获取单个字段的输入框元素
                const measureDate = document.getElementById('measureDate');
                if (measureDate.value.trim() === '') {
                    layer.msg('称重日期不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const earNnumber = document.getElementById('earNumber');

                if (earNnumber.value.trim() === '') {
                    layer.msg('羊只耳号不能为空！');
                    return;
                }

                // 获取单个字段的输入框元素
                const height = document.getElementById('height');

                if (height.value.trim() === '') {
                    layer.msg('身高不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const bodyLength = document.getElementById('bodyLength');

                if (bodyLength.value.trim() === '') {
                    layer.msg('体长不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const chestWidth = document.getElementById('chestWidth');

                if (chestWidth.value.trim() === '') {
                    layer.msg('胸围不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const cannonCircumference = document.getElementById('cannonCircumference');

                if (cannonCircumference.value.trim() === '') {
                    layer.msg('管围不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const scrotalCircumference = document.getElementById('scrotalCircumference');

                if (scrotalCircumference.value.trim() === '') {
                    layer.msg('阴囊围不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const backfatThickness = document.getElementById('backfatThickness');

                if (backfatThickness.value.trim() === '') {
                    layer.msg('背膘厚度不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const eyeMuscleArea = document.getElementById('eyeMuscleArea');

                if (eyeMuscleArea.value.trim() === '') {
                    layer.msg('眼肌面积不能为空！');
                    return;
                }
                // 获取单个字段的输入框元素
                const eyeMuscleWidth = document.getElementById('eyeMuscleWidth');

                if (eyeMuscleWidth.value.trim() === '') {
                    layer.msg('眼肌宽度不能为空！');
                    return;
                }
            }
        });

        return false; // 阻止默认 form 提交
    });

    //绑定羊只耳号的枚举
    $(document).ready(function () {
        $.ajax({
            url: `http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesAllInfo`,
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var enumData = res; // 直接使用返回的数据
                var select = $('#earNumber');

                $.each(enumData, function (index, item) {
                    if (item.sheepType == "后裔") {
                        // 使用正确的字段添加选项到下拉框中
                        var option = $('<option>').val(item.earNnumber).text(item.earNnumber);
                        select.append(option);
                    }
                });
                // 手动触发 layui 组件的渲染
                layui.form.render('select');
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });
    // 根据羊只耳号获取羊只详情
    form.on('select(sheepSelect)', function (data) {
        $.ajax({
            url: `http://10.31.60.18:8000/ArchivesGetRout/api/Archives/ArchivesGetByEarNumberInfo?EarNumber=` + data.value,
            type: 'get',
            dataType: 'json',
            success: function (res) {
                $("#sheepGender").val(res.sheepGender);
                $("#sheepBreed").val(res.sheepBreed);
                $("#cottageId").val(res.cottageId);
                $("#field").val(res.field);
            },
            error: function () {
                // 处理 AJAX 请求失败的情况
                console.error('Failed to fetch data for dropdown.');
            }
        });
    });
})